var GameCreator = function(settings)
{
$("#instructions").hide("fast");
var thisObj = this;

this.canvas = settings.canvas;
this.context = this.canvas.getContext('2d');

		this.flippSwitch = 0;

			$("#instrClick").click(function(){
				if(thisObj.flippSwitch++ % 2 == 1)
				{
				$("#instructions").hide("fast");
				$("#instrClick").val("+").blur();
				}
				else
				{
				$("#instructions").show("fast");
				$("#instrClick").val("-").blur();
				}
			},thisObj);
			
		this.gameEngine;
		this.setGamePlay;
		this.initiatePaint = function(){
		
			$("#instructions").hide();
			$("#newGame").get(0).disabled = false;
			$("#resetScores").get(0).disabled = false;	
			
			if(readCookie("level"))
			{
			this.level = parseInt(readCookie("level"));
			}
			else
			{
			this.level = 0;
			}
		this.canvas.width = 350;
		this.canvas.height = 50;
		this.context.fillStyle = "rgb(120,120,120)";
		this.context.fillRect(0,0,this.canvas.width,this.canvas.height);
		this.context.fillStyle = "rgb(255,255,255)";
		this.context.font = '400 19px/2 Unknown Font, sans-serif';		
		var message2 = "You are currently at level "+this.level+""
		this.context.fillText(message2,this.canvas.width/2-this.context.measureText(message2).width/2,this.canvas.height/2+10);
		
		
		this.setGamePlay = new GamePlay({level:this.level,patternOdds:1,difficulty:1});

			$("html").keypress(function(event) {
				if (event.keyCode == '32' || event.keyCode == '38'|| event.keyCode == '40') {
				thisObj.createNewGame();
				return false;
   				}   				
			});
			
			$("#newGame").click(
				function(){
				thisObj.createNewGame();
				$(this).blur();
				}
			);
			
			$("#resetScores").click(
				function(){
					if(confirm("Reset game progress?"))
					{
					eraseCookie("level");
					thisObj.setGamePlay = new GamePlay({level:0,patternOdds:1,difficulty:1});
					thisObj.context.fillStyle = "rgb(120,120,120)";
					thisObj.context.fillRect(0,0,thisObj.canvas.width,thisObj.canvas.height);
					thisObj.context.fillStyle = "rgb(255,255,255)";
					thisObj.context.font = '400 19px/2 Unknown Font, sans-serif';		
					var message = "Scores reset";
					thisObj.context.fillText(message,thisObj.canvas.width/2-thisObj.context.measureText(message).width/2,thisObj.canvas.height/2);					
					}
				}
			)
			
			$("#gameAbort").click(
				function(){
				thisObj.restart();
				thisObj.context.fillStyle = "rgb(120,120,120)";
				thisObj.context.fillRect(0,0,thisObj.canvas.width,thisObj.canvas.height);
				thisObj.context.fillStyle = "rgb(255,255,255)";
				thisObj.context.font = '400 19px/2 Unknown Font, sans-serif';		
				var message = "Game aborted";
				thisObj.context.fillText(message,thisObj.canvas.width/2-thisObj.context.measureText(message).width/2,thisObj.canvas.height/2);
				var message = "[Press space/up to continue]";
				thisObj.context.fillText(message,thisObj.canvas.width/2-thisObj.context.measureText(message).width/2,thisObj.canvas.height/2+20);
				}
			)			
      	}
      	this.restart = function(){
					if(this.gameEngine)
					{
					this.gameEngine.cleanUp();
					this.gameEngine = null;
					}
				}
		
		this.createNewGame = function(){

			$("#newGame").get(0).disabled = true;
			$("#resetScores").get(0).disabled = true;
			
			if(this.gameEngine!=null)
			{
			this.gameEngine.cleanUp();
			this.gameEngine = null;
			}
      		this.gameEngine = new GameEngine({timePerFrameWanted:30,squareWidth:(70-this.setGamePlay.patternSize*5),squareHeight:(70-this.setGamePlay.patternSize*5),gamePlay:this.setGamePlay,patternColumns:(this.setGamePlay.nBack+2),patternRows:1,creator:this});
			this.gameEngine.startPaint();

			$("html").unbind("keypress");			
			$("html").keypress(function(event) {
				if (event.keyCode == '113' || event.keyCode == '37') {
    	 		thisObj.gameEngine.patternPile[thisObj.gameEngine.patternPile.length-1].positive.rotate(-1);
    	 		return false;
   				}
				if (event.keyCode == '97' || event.keyCode == '39') {
    	 		thisObj.gameEngine.patternPile[thisObj.gameEngine.patternPile.length-1].positive.rotate(1);
    	 		return false;
   				}
				if (event.keyCode == '32' || event.keyCode == '38'|| event.keyCode == '40') {
    	 		thisObj.gameEngine.patternPile[thisObj.gameEngine.patternPile.length-1].positive.tryContinue();
    	 		return false;
   				} 		
			});
		}
this.initiatePaint();
}